<?php

require_once(SITE_ROOT . 'lib/PHT.php');

class Registration {

	function login($user, $password) {
		try {
			$HT = new CHPPConnection(VERSION, CHPPID, CHPPKEY, SITE_ROOT . 'tmp/hattrick/');
			if ($HT->connectUser($user, $password)) {
				$_SESSION['login'] = true;
				$_SESSION['login_teamID'] = $HT->getTeam()->getTeamId();
				$_SESSION['HT'] = $HT;
			}
			else {
				unset($_SESSION['login']);
				unset($_SESSION['HT']);
				throw new Exception('Login failed!');
			}
		}
		catch (Exception $e) {
			unset($_SESSION['login']);
			unset($_SESSION['login_teamID']);
			unset($_SESSION['HT']);
			throw new Exception('Login failed!');
		}
	}

	function logout() {
		unset($_SESSION['login']);
		unset($_SESSION['login_teamID']);
		$HT = $_SESSION['HT'];
		if ($HT instanceof CHPPConnection) {
			$HT->disconnect();
		}
		else throw new Exception('Timeout!');
		unset($_SESSION['HT']);
	}
}

?>